package cn.tedu.charging.user.dao.repository;

import cn.tedu.charging.common.pojo.param.ChargeParam;
import cn.tedu.charging.common.pojo.po.ChargingUserInfoPO;
import cn.tedu.charging.user.dao.mapper.ChargingUserInfoMapper;
import cn.tedu.charging.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

@Repository
public class UserRepository {
    @Autowired
    private ChargingUserInfoMapper chargingUserInfoMapper;


    public ChargingUserInfoPO getUserByOpenId(String openId) {
        QueryWrapper<ChargingUserInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wx_open_id",openId);
       return chargingUserInfoMapper.selectOne(queryWrapper);
    }

    public void insertUser(ChargingUserInfoPO userInfoPO) {
        chargingUserInfoMapper.insert(userInfoPO);
    }

    public ChargingUserInfoPO getUserById(Integer userId) {
        return chargingUserInfoMapper.selectById(userId);
    }

    public void updateUserBalance(Integer userId, BigDecimal amount) {
        UpdateWrapper<ChargingUserInfoPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",userId);
        updateWrapper.setSql("balance=balance+"+amount);
        chargingUserInfoMapper.update(null,updateWrapper);
    }
}
